//
//  LoginViewController.m
//  Toyohu
//
//  Created by Heige on 16/4/16.
//  Copyright © 2016年 celia. All rights reserved.
//

#import "LoginViewController.h"
#import "XHToast.h"
#import "SVProgressHUD.h"
#import "BabySetViewController.h"
#import "GPCustomMD.h"

#import "GPAFNetworking3.h"
#import "TYH_LoginLoginModel.h"
#import "LoginManager.h"

#import "CYLTabBarControllerConfig.h"

@interface LoginViewController ()<UITextFieldDelegate>
{
    UIView *tipsView;
}
@property (weak, nonatomic) IBOutlet UIImageView *phoneImg;
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UIImageView *SureImg;
@property (weak, nonatomic) IBOutlet UITextField *Sure;
@property (weak, nonatomic) IBOutlet UIButton *yzButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *wxLogin;
@property (weak, nonatomic) IBOutlet UIButton *qqLogin;
@property (weak, nonatomic) IBOutlet UIButton *wbLogin;
@property (weak, nonatomic) IBOutlet UIView *phoneBackView;
@property (weak, nonatomic) IBOutlet UIView *sureBackView;
@property(nonatomic, copy) NSString *oUserPhoneNum;
@property(assign, nonatomic) NSInteger timeCount;
@property(strong, nonatomic) NSTimer *timer;
//验证码
@property(copy, nonatomic) NSString *smsId;
@end

@implementation LoginViewController

- (void)isLogin
{
    if ([[LoginManager shareLoginManager] isLoggedin]) {
        CYLTabBarControllerConfig *tabBarControllerConfig = [[CYLTabBarControllerConfig alloc] init];
        [[[UIApplication sharedApplication] keyWindow] setRootViewController:tabBarControllerConfig.tabBarController];
        return;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self isLogin];

    self.navigationController.navigationBar.hidden = YES;


    
    self.phoneBackView.layer.cornerRadius = self.phoneBackView.frame.size.height/2;
    self.phoneBackView.clipsToBounds = YES;
    self.phoneBackView.layer.borderWidth = 1;
    self.phoneBackView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    self.sureBackView.layer.cornerRadius = self.phoneBackView.frame.size.height/2;
    self.sureBackView.clipsToBounds = YES;
    self.sureBackView.layer.borderWidth = 1;
    self.sureBackView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    self.loginButton.layer.cornerRadius = self.phoneBackView.frame.size.height/2;
    self.loginButton.clipsToBounds = YES;
    [self.loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    self.phone.delegate = self;
    self.Sure.delegate  =self;
    [self.yzButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.yzButton addTarget:self action:@selector(getValidCode:) forControlEvents:UIControlEventTouchUpInside];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keyBoard)];
    [self.view addGestureRecognizer:tap];
    
}

-(void)keyBoard
{
    [self.phone resignFirstResponder];
    [self.Sure resignFirstResponder];
}

- (void)getValidCode:(UIButton *)sender
{
    if ([self.phone.text isEqualToString:@""])
    {
        return;
    }
    else if (self.phone.text.length <11)
    {
        return;
    }
    _oUserPhoneNum =self.phone.text;
    sender.userInteractionEnabled = NO;
    self.timeCount = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reduceTime:) userInfo:sender repeats:YES];
    
    [GPAFNetworking3 post:GETCODE parameters:@{@"phoneNum":_oUserPhoneNum} progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nullable responseObject) {
        
    } failure:^(NSString * _Nonnull errorLD) {
        
    }];
    
}

- (void)reduceTime:(NSTimer *)codeTimer {
    self.timeCount--;
    if (self.timeCount == 0) {
        [self.yzButton setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.yzButton setTitleColor:[UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.0f alpha:1] forState:UIControlStateNormal];
        UIButton *info = codeTimer.userInfo;
        info.enabled = YES;
        self.yzButton.userInteractionEnabled = YES;
        [self.timer invalidate];
    } else {
        NSString *str = [NSString stringWithFormat:@"重新获取(%ld)秒", (long)self.timeCount];
        [self.yzButton setTitle:str forState:UIControlStateNormal];
        self.yzButton.userInteractionEnabled = NO;
        
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.phone resignFirstResponder];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//验证码
-(void)login
{
    
    if ([self.phone.text isEqualToString:@""]||self.phone.text.length <11)
    {
        [XHToast showTopWithText:@"手机号码有误" topOffset:90.0];
        self.phoneBackView.layer.borderColor = [UIColor colorWithRed:217/255.0 green:96/255.0 blue:21/255.0 alpha:1].CGColor;
        return;
    }
    else if ([self.Sure.text isEqualToString:@""])
    {
        [XHToast showTopWithText:@"验证码错误" topOffset:90.0];
        self.sureBackView.layer.borderColor =[UIColor colorWithRed:217/255.0 green:96/255.0 blue:21/255.0 alpha:1].CGColor;
        return;
    }

    else
    {
//        [XHToast showTopWithText:@"正在登陆中..." topOffset:90.0 duration:1.0 color:[UIColor colorWithRed:20/255.0 green:148/255.0 blue:121/255.0 alpha:1]];
        [SVProgressHUD showWithStatus:@"正在登陆中..." maskType:SVProgressHUDMaskTypeGradient];
        [GPAFNetworking3 post:Login parameters:@{@"info":[GPAFNetworking3 dictionaryToJson:@{@"phoneNum":self.phone.text,@"openId":@"",@"headUrl":@"",@"validCode":@"1234",@"location":@"",@"channel":@"toyohu"}]} progress:nil success:^(id  _Nullable responseObject) {
            TYH_LoginLoginModel *loginModel = [[TYH_LoginLoginModel alloc] initWithDictionary:responseObject];
            if (loginModel.code == 1) {
                [SVProgressHUD dismiss];
                GPSET(responseObject, @"loginInfo")
                if (loginModel.msg.newUser == 0) {
                    [[[UIApplication sharedApplication] keyWindow] setRootViewController:[BabySetViewController new]];
                    GPSET(self.phone.text, @"user_phone")

                } else {
                        CYLTabBarControllerConfig *tabBarControllerConfig = [[CYLTabBarControllerConfig alloc] init];
                        [[[UIApplication sharedApplication] keyWindow] setRootViewController:tabBarControllerConfig.tabBarController];
                }
            } else {
                SHOW_ERROR
            }
            
        } failure:^(NSString * _Nonnull errorLD) {
            SHOW_NTERROR
        }];
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    self.phoneBackView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.Sure.layer.borderColor = [UIColor whiteColor].CGColor;
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.phoneBackView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.Sure.layer.borderColor = [UIColor whiteColor].CGColor;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
